package org.openclinic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.openclinic.domain.Customer;
import org.openclinic.exception.AbstractExceptionHandler;
import org.openclinic.exception.DomainException;
import org.openclinic.service.CustomerService;
import org.openclinic.service.HealthInsuranceService;
import org.openclinic.service.ReminderService;
import org.openclinic.to.CustomerSearchResult;
import org.openclinic.to.DataTablesTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes("customer")
public class CustomerController extends AbstractExceptionHandler {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private HealthInsuranceService insuranceService;

	@Autowired
	private ReminderService reminderService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, "birthdate", new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
	}
	
	@ResponseBody
	@RequestMapping(value = "/customer/delete.action", method = RequestMethod.POST)
	public boolean delete(@RequestParam long id) {
		customerService.delete(id);
		return true;
	}
	
	@RequestMapping(value = "/customer.do", method = RequestMethod.GET)
	public ModelAndView init() {
		ModelAndView modelAndView = new ModelAndView("user.view");
		modelAndView.addObject("userDesignation", "Cliente");
		modelAndView.addObject("bond", "customer");
		
		return modelAndView;
	}

	@RequestMapping(value = "/customer/open.diag", method = RequestMethod.POST)
	public ModelAndView initNewForm(@RequestParam Long id) {	
		ModelAndView modelAndView = new ModelAndView("user-form.jsp");
		modelAndView.addObject("bond", "customer");
		modelAndView.addObject("insuranceList", insuranceService.list());
		
		if (id == null) {
			modelAndView.addObject("customer", new Customer());
		} else {
			modelAndView.addObject("customer", customerService.find(id));
		}
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/customer/openReminders.diag", method = RequestMethod.POST)
	public ModelAndView initRemindersForm(@RequestParam long id) {	
		ModelAndView modelAndView = new ModelAndView("reminder-form.jsp");
		modelAndView.addObject("reminderList", reminderService.findByCustomer(id));
		
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value = "/customer/list.action", method = RequestMethod.POST)
	public DataTablesTO<Customer> list() {
		return new DataTablesTO<Customer>(customerService.list());
	}
	
	@ResponseBody
	@RequestMapping(value = "/customer/save.action", method = RequestMethod.POST)
	public Customer save(@Valid @ModelAttribute Customer customer, BindingResult validationResult) {
		if(!validationResult.hasErrors()) {
			customerService.save(customer);
			customer.setHealthInsurance(insuranceService.find(customer.getHealthInsurance().getInsuranceId()));
			return customer;
		}
		throw new DomainException(validationResult.getAllErrors());
	}
	
	@ResponseBody
	@RequestMapping(value = "/customer/search.action", method = RequestMethod.GET)
	public List<CustomerSearchResult> search(@RequestParam String term) {
		return customerService.search(term);
	}
}